// bflat minimal runtime library
// Copyright (C) 2021-2022 Michal Strehovsky
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

namespace System
{
    public struct Nullable<T> where T : struct
    {
        private readonly bool _hasValue;
        private T _value;

        public Nullable(T value) => (_hasValue, _value) = (true, value);

        public readonly bool HasValue => _hasValue;

        public readonly T Value
        {
            get
            {
                if (!_hasValue)
                    Environment.FailFast(null);
                return _value;
            }
        }

        public static implicit operator Nullable<T>(T value) => new Nullable<T>(value);

        public static explicit operator T(Nullable<T> value) => value.Value;
    }
}
